package me.keiwu.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.keiwu.admin.rpc.ItemRpc;
import me.keiwu.pub.data.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by kei on 30/11/2017.
 */
@RestController
@RequestMapping("/api/item")
public class ItemController {

    @Autowired
    private ItemRpc itemRpc;


    @ResponseBody
    @RequestMapping("/suit/list")
    public Response listSuit(@RequestParam(defaultValue = "0") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        JSONObject itemList = JSON.parseObject(itemRpc.listSuit(page, size));
        return Response.ok(itemList);
    }

    @ResponseBody
    @RequestMapping("/suit/add")
    public Response addSuit(@RequestParam("name") String name,
                    @RequestParam("imgUrl") String imgUrl,
                    @RequestParam("itemDetail") String itemDetail,
                    @RequestParam("costGold") int costGold,
                    @RequestParam("shareGet") boolean shareGet,
                    @RequestParam(value = "shelve", defaultValue = "true") boolean shelve) {
        JSONObject json = JSON.parseObject(itemRpc.addSuit(name, imgUrl, itemDetail, costGold, shareGet, shelve));
        return Response.ok(json);
    }

    @ResponseBody
    @RequestMapping("/suit/update")
    public Response updateSuit(@RequestParam("suitId") Integer suitId,
                       @RequestParam("name") String name,
                       @RequestParam("imgUrl") String imgUrl,
                       @RequestParam("itemDetail") String itemDetail,
                       @RequestParam("costGold") int costGold,
                       @RequestParam("shareGet") boolean shareGet,
                       @RequestParam(value = "shelve", defaultValue = "true") boolean shelve) {
        JSONObject json = JSON.parseObject(itemRpc.updateSuit(suitId, name, imgUrl, itemDetail, costGold, shareGet, shelve));
        return Response.ok(json);
    }






    // 单件
    @ResponseBody
    @RequestMapping("/single/list")
    public Response listSingle(@RequestParam("page") int page, @RequestParam("size") int size) {
        JSONObject json = JSON.parseObject(itemRpc.listSingle(page, size));
        return Response.ok(json);
    }

    @ResponseBody
    @RequestMapping("/single/add")
    public Response addSingle(@RequestParam("name") String name,
                              @RequestParam("imgUrl") String imgUrl,
                              @RequestParam("costGold") int costGold,
                              @RequestParam("shareGet") boolean shareGet,
                              @RequestParam(value = "shelve", defaultValue = "true") boolean shelve,
                              @RequestParam("combItemsJson") String combItemsJson) {
        JSONObject json = JSON.parseObject(itemRpc.addSingle(name, imgUrl, costGold, shareGet, shelve, combItemsJson));
        return Response.ok(json);
    }


    @ResponseBody
    @RequestMapping("/single/update")
    public Response updateSingle(@RequestParam("singleId") Integer singleId,
                                 @RequestParam("name") String name,
                                 @RequestParam("imgUrl") String imgUrl,
                                 @RequestParam("costGold") int costGold,
                                 @RequestParam("shareGet") boolean shareGet,
                                 @RequestParam(value = "shelve", defaultValue = "true") boolean shelve,
                                 @RequestParam("combItemsJson") String combItemsJson) {
        JSONObject json = JSON.parseObject(itemRpc.updateSingle(singleId, name, imgUrl, costGold, shareGet, shelve, combItemsJson));
        return Response.ok(json);
    }





    @ResponseBody
    @RequestMapping("/shelve")
    public Response shelve(@RequestParam("itemId") Integer itemId, @RequestParam("shelve") boolean shelve) {
        return Response.ok(itemRpc.shelve(itemId, shelve));
    }

}
